import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import {
    getAvatarList,
    getAvatarStats,
    triggerSync,
    getExcludeList,
    addExclude,
    removeExclude,
    batchDeleteExclude, getAvatarById
} from '@/api/video/avatar'
import { ElMessage } from 'element-plus'

export const useAvatarStore = defineStore('avatar', () => {
    const avatars = ref([])
    const avatar = ref([])
    const avatarStats = ref({})
    const loading = ref(false)
    const excludeList = ref([])
    const total = ref(0)
    const totalExclude = ref(0)

    // 排除名单分页参数
    const excludeQuery = reactive({
        pageNum: 1,
        pageSize: 12,
        avatarName: ''
    })

    // 获取虚拟人物列表
    const fetchAvatars = async (params) => {
        try {
            loading.value = true
            const { data } = await getAvatarList(params)
            avatars.value = data.records
            total.value = data.total
        } catch (error) {
            ElMessage.error(error.message || '获取虚拟人物列表失败')
        } finally {
            loading.value = false
        }
    }

    // 获取统计数据
    const fetchStats = async () => {
        try {
            const { data } = await getAvatarStats()
            avatarStats.value = data
        } catch (error) {
            ElMessage.error(error.message || '获取统计数据失败')
        }
    }

    // 获取统计数据
    const fetchById = async (id) => {
        try {
            loading.value = true
            const { data } = await getAvatarById(id)
            avatar.value = data
            return data
        } catch (error) {
            ElMessage.error(error.message || '获取角色数据失败')
        } finally {
            loading.value = false
        }
    }

    // 手动触发同步
    const manualSync = async () => {
        try {
            loading.value = true
            await triggerSync()
            ElMessage.success('同步触发成功')
            // 重新加载数据
            await Promise.all([
                fetchAvatars(),
                fetchStats()
            ])
        } catch (error) {
            ElMessage.error(error.message || '同步触发失败')
            throw error
        } finally {
            loading.value = false
        }
    }

    // 获取排除名单（分页）
    const fetchExcludeList = async () => {
        try {
            const { data } = await getExcludeList(excludeQuery)
            excludeList.value = data.records
            totalExclude.value = data.total
        } catch (error) {
            ElMessage.error(error.message || '获取排除名单失败')
            throw error
        }
    }

    // 添加排除名单
    const addExcludeName = async (data) => {
        try {
            await addExclude(data)
            ElMessage.success('添加成功')
            await fetchExcludeList()
        } catch (error) {
            ElMessage.error(error.message || '添加排除名单失败')
            throw error
        }
    }

    // 删除排除名单
    const removeExcludeName = async (id) => {
        try {
            await removeExclude(id)
            ElMessage.success('删除成功')
            await fetchExcludeList()
        } catch (error) {
            ElMessage.error(error.message || '删除排除名单失败')
            throw error
        }
    }

    // 批量删除排除名单
    const batchDeleteExcludeName = async (ids) => {
        try {
            await batchDeleteExclude(ids)
            ElMessage.success('删除成功')
            await fetchExcludeList()
        } catch (error) {
            ElMessage.error(error.message || '批量删除失败')
            throw error
        }
    }

    return {
        avatars,
        avatar,
        avatarStats,
        loading,
        excludeList,
        total,
        totalExclude,
        excludeQuery,
        fetchAvatars,
        fetchStats,
        manualSync,
        fetchExcludeList,
        addExcludeName,
        removeExcludeName,
        batchDeleteExcludeName,
        fetchById
    }
})